package decisionLines.model;

import java.util.ArrayList;

/**
 * Class for Line
 */
public class Line {
	String choice;
	int position;
	ArrayList<Edge> edges = new ArrayList<Edge>();
	
	/**
	 * Constructor: Line
	 * @param position - The position of the line (int)
	 */
	public Line(int position){
		this.position = position;
	}
	
	/**
	 * Method: retrieves the position of the Line
	 * @return position - The position of the Line (int)
	 */
	public int getPosition() {
		return this.position;
	}
	
	/**
	 * Method: set the choice associated with this line
	 * @param choice - The choice to set with the line (string)
	 */
	public boolean setChoice(String choice){
		//If choice is an empty string then return an "error"
		if(choice.equals("")){
			return false;
		}
		this.choice = choice;
		return true;
	}
	
	/**
	 * Method: retrieves the choice associated with this line
	 * @return String - Choice of the line
	 */
	public String getChoice(){
		return choice;
	}
	
	/**
	 * Method: retrieve next edge
	 * @param startingHeight - The current height during downward traversal on the line (int)
	 * @return Edge: Edge being retrieved, which will have greater height (downward) than the currentHeight.
	 */
	public Edge nextEdge(int startingHeight){
		//Temporary edge variable to be returned when next edge is found
		Edge followingEdge = null;

		//Test each edge in the array list
		for(int i=0; i<edges.size(); i++){
			//If the edge is greater than the starting height then test against the currently stored edge
			if(edges.get(i).getHeight() > startingHeight){
				//If the edge is closer than the previously stored edge then make the closest edge
				if((followingEdge == null) || (edges.get(i).getHeight() < followingEdge.getHeight())){
					followingEdge = edges.get(i);
				}
			}
		}
		
		return followingEdge;
	}
	
	/**
	 * Method: add an edge to the line
	 * @param newEdge - Edge to be added to the line (Edge)
	 * @return boolean: If false, then failed to add the edge to the array list of edges; true if added.
	 */
	public boolean addEdge(Edge newEdge){
		if (!(edges.contains(newEdge)) && edges.add(newEdge)){
			return true;
		}
		return false;
	}
	
	/**
	 * Method: Removes an edge from the line
	 * @param newEdge - Edge, the edge being removed
	 * 
	 * During a validity check from DecisionLinesEvent, an edge may have been added to the line
	 * that was not a valid edge due to a conflict with another line, therefore it may be possible
	 * that an edge must be removed
	 */
	public void removeEdge(Edge newEdge){
		//If the edge exists then remove it (may throw error exception without this)
		if(edges.contains(newEdge)){
			edges.remove(newEdge);
		}
	}
}
